package com.rdc.qingkou.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.R;
import com.rdc.qingkou.ui.BrowseActivity;
import com.rdc.qingkou.vo.CardVO;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;


public class HomepageFragment extends BaseMediaPlayerFragment {

    private String tag;
    private CardVO card;
    private JZVideoPlayerStandard player;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }


    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            card = (CardVO) savedInstanceState.getSerializable("cardVO");
            tag = savedInstanceState.getString("tag");
        }
        View view = inflater.inflate(R.layout.item_homepage_card, container, false);
        ImageView cardImage = (ImageView) view.findViewById(R.id.layout_card_ImageView);
        ImageView voice = (ImageView) view.findViewById(R.id.layout_card_voice_imageView);
        player = (JZVideoPlayerStandard) view.findViewById(R.id.layout_card_videoPlayer);
        TextView content = (TextView) view.findViewById(R.id.layout_card_content_textView);
        TextView title = (TextView) view.findViewById(R.id.item_homepage_card_title_textView);
        if (card.getMediaUrl() != null) {
            //有视频,则不显示图片和音频
            cardImage.setVisibility(View.GONE);
            voice.setVisibility(View.GONE);
            player.setVisibility(View.VISIBLE);
            player.setUp(card.getMediaUrl(), JZVideoPlayer.SCREEN_LAYOUT_LIST, "");
            //设置封面
            //mPlayer.thumbImageView.setImageBitmap(MyUtils.getNetVideoBitmap(URL.HEAD_YUN + mCard.getMediaUrl()));
        } else {
            Glide.with(this).load(card.getImageUrl()).placeholder(R.drawable.imageview_default).into(cardImage);
            if (card.getVoiceUrl() != null) {
                voiceUrl = card.getVoiceUrl();
                voice.setOnClickListener(this);
            } else {
                voice.setVisibility(View.GONE);
            }
        }
        content.setText(card.getContent());
        title.setText(tag);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), BrowseActivity.class);
                intent.putExtra("tag", tag);
                startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("cardVO", card);
        outState.putString("tag", tag);
    }

    public void setCard(CardVO card, String tag) {
        this.card = card;
        this.tag = tag;
    }
}
